package oot.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSonPaser {
	public static User paserUser(JSONObject obj) throws JSONException {
		User result = new User("", "");
		result.setUserName(obj.getString("username"));
		if (obj.toString().contains("accesstoken"))
			result.setAccesstoken(obj.getString("accesstoken"));
		result.setUserID(obj.getString("id"));
		result.setCreatedDate(obj.getString("created"));
		result.setUserAvatarURL(obj.getString("avatar"));
		// Log.i("test","avata url"+result.getUserAvatarURL());
		// Log.d("test","obj:"obj.)
		result.setUserScreenName(obj.getString("screen_name"));
		result.setFollowStatus(obj.optBoolean("followStatus"));
		return result;
	}

	public static Tweet paserTweet(JSONObject tempTweetJS) {
		Tweet tempTweet = new Tweet();
		try {
			tempTweet.setType((tempTweetJS.getJSONObject("Tweet"))
					.getString("type"));
			if ((tempTweet.getType()).equals("3")) {// is retweet
				Log.i("test", "Runhere");
				JSONObject retweet = tempTweetJS.getJSONObject("Retweet");
				Tweet tempRetweet = new Tweet();
				tempRetweet = paserReTweet(retweet);
				Log.i("test", "Content :" + tempRetweet.getContent());
				tempRetweet.setUser(JSonPaser.paserUser(tempTweetJS
						.getJSONObject("ParentUser")));
				// Log.i("test","retweet:"+ tempRetweet.getContent());
				tempTweet.setRetweet(tempRetweet);
			}
			User tempUser = new User("", "");
			tempUser = paserUser(tempTweetJS.getJSONObject("User"));
			tempTweet.setImgURL(tempTweetJS.getJSONObject("Tweet").getString(
					"image_url"));
			tempTweet.setParentTweetId(tempTweetJS.getJSONObject("Tweet")
					.getString("parent_id"));
			tempTweet.setTweetId(tempTweetJS.getJSONObject("Tweet").getString(
					"id"));
			tempTweet
					.setContent(tempTweetJS.getJSONObject("Tweet").getString(
							"content")
							+ " "
							+ tempTweetJS.getJSONObject("Tweet").getString(
									"image_url"));
			tempTweet.setUser(tempUser);
			tempTweet.setCreatedDate(tempTweetJS.getJSONObject("Tweet")
					.getString("created"));
			tempTweet.setTimeSince(tempTweetJS.getJSONObject("0").getString(
					"leftTime"));
			tempTweet.setFavorite(tempTweetJS.getJSONObject("0").getString(
					"favorite"));
			tempTweet.setReTweetedByMe((tempTweetJS.getJSONObject("0")
					.getString("retweetedbyme")));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tempTweet = new Tweet(new User("error", "error"), "ms error");
		}
		return tempTweet;
	}

	public static Tweet paserReTweet(JSONObject tempTweetJS)
			throws JSONException {
		Tweet tempTweet = new Tweet();
		tempTweet.setImgURL(tempTweetJS.getString("image_url"));
		tempTweet.setParentTweetId(tempTweetJS.getString("parent_id"));
		tempTweet.setTweetId(tempTweetJS.getString("id"));
		tempTweet.setContent(tempTweetJS.getString("content") + " "
				+ tempTweetJS.getString("image_url"));
		tempTweet.setCreatedDate(tempTweetJS.getString("created"));
		return tempTweet;
	}

	public static ArrayList<User> listFollowers(String res, ArrayList<User> list) {
		ArrayList<User> listFollowers = list;
		try {
			JSONObject jsonObject = new JSONObject(res);
			if (jsonObject.getInt("status_code") == 0) {
				JSONArray jsonArray = jsonObject.getJSONArray("entity");
				for (int i = 0; i < jsonArray.length(); i++) {
					User user = new User();
					JSONObject userJsonObject = jsonArray.getJSONObject(i)
							.getJSONObject("FollowingUser");
					user.setUserName(userJsonObject.getString("username"));
					user.setUserID(userJsonObject.getString("id"));
					user.setUserAvatarURL(userJsonObject.getString("avatar"));
					user.setUserEmail(userJsonObject.getString("email"));
					user.setUserScreenName(userJsonObject
							.getString("screen_name"));
					user.setFollowStatus(userJsonObject
							.getBoolean("followStatus"));
					listFollowers.add(user);
				}

				return listFollowers;
			} else {
				return null;
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static ArrayList<User> listFollowings(String res,
			ArrayList<User> list) {
		ArrayList<User> listFollowers = list;
		try {
			JSONObject jsonObject = new JSONObject(res);
			if (jsonObject.getInt("status_code") == 0) {
				JSONArray jsonArray = jsonObject.getJSONArray("entity");
				for (int i = 0; i < jsonArray.length(); i++) {
					User user = new User();
					JSONObject userJsonObject = jsonArray.getJSONObject(i)
							.getJSONObject("FollowerUser");
					user.setUserName(userJsonObject.getString("username"));
					user.setUserID(userJsonObject.getString("id"));
					user.setUserAvatarURL(userJsonObject.getString("avatar"));
					user.setUserEmail(userJsonObject.getString("email"));
					user.setUserScreenName(userJsonObject
							.getString("screen_name"));
					user.setFollowStatus(userJsonObject
							.getBoolean("followStatus"));
					listFollowers.add(user);
				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return listFollowers;
	}

	public static ArrayList<User> listFavorites(String res, ArrayList<User> list) {
		ArrayList<User> listFollowers = list;
		try {
			JSONObject jsonObject = new JSONObject(res);
			if (jsonObject.getInt("status_code") == 0) {
				JSONArray jsonArray = jsonObject.getJSONArray("entity");
				for (int i = 0; i < jsonArray.length(); i++) {
					User user = new User();
					JSONObject userJsonObject = jsonArray.getJSONObject(i)
							.getJSONObject("User");
					user.setUserName(userJsonObject.getString("username"));
					user.setUserID(userJsonObject.getString("id"));
					user.setUserAvatarURL(userJsonObject.getString("avatar"));
					user.setUserEmail(userJsonObject.getString("email"));
					user.setUserScreenName(userJsonObject
							.getString("screen_name"));
					user.setFollowStatus(userJsonObject
							.getBoolean("followStatus"));
					listFollowers.add(user);
				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return listFollowers;
	}

	public static ArrayList<User> listSuggests(String res, ArrayList<User> list) {
		ArrayList<User> listSuggests = list;
		try {
			JSONObject jsonObject = new JSONObject(res);
			if (jsonObject.getInt("status_code") == 0) {
				JSONArray jsonArray = jsonObject.getJSONArray("entity");
				for (int i = 0; i < jsonArray.length(); i++) {
					User user = new User();
					JSONObject userJsonObject = jsonArray.getJSONObject(i)
							.getJSONObject("User");
					user.setUserName(userJsonObject.getString("username"));
					user.setUserID(userJsonObject.getString("id"));
					user.setUserAvatarURL(userJsonObject.getString("avatar"));
					user.setUserEmail(userJsonObject.getString("email"));
					user.setUserScreenName(userJsonObject
							.getString("screen_name"));
					// user.setFollowStatus(userJsonObject.getBoolean("followStatus"));
					user.setFollowStatus(false);
					listSuggests.add(user);
				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return listSuggests;
	}

	public static ArrayList<User> listRetweets(String res, ArrayList<User> list) {
		ArrayList<User> listRetweets = list;
		try {
			JSONObject jsonObject = new JSONObject(res);
			if (jsonObject.getInt("status_code") == 0) {
				JSONArray jsonArray = jsonObject.getJSONArray("entity");
				for (int i = 0; i < jsonArray.length(); i++) {
					User user = new User();
					JSONObject userJsonObject = jsonArray.getJSONObject(i)
							.getJSONObject("User");
					user.setUserName(userJsonObject.getString("username"));
					user.setUserID(userJsonObject.getString("id"));
					user.setUserAvatarURL(userJsonObject.getString("avatar"));
					user.setUserEmail(userJsonObject.getString("email"));
					user.setUserScreenName(userJsonObject
							.getString("screen_name"));
					user.setFollowStatus(userJsonObject
							.getBoolean("followStatus"));
					listRetweets.add(user);
				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return listRetweets;
	}

	public static ArrayList<Place> getPlace(String json) {
		ArrayList<Place> listPlace = new ArrayList<Place>();
		try {
			JSONArray jsonArray = new JSONObject(json).getJSONArray("data");
			if (jsonArray != null) {
				for (int i = 0; i < jsonArray.length(); i++) {
					Place place = new Place();
					if (jsonArray.getJSONObject(i).getString("name") != null) {
						place.setName(jsonArray.getJSONObject(i).getString(
								"name"));
					}
					if (jsonArray.getJSONObject(i).getString("id") != null) {
						place.setIdPlace(jsonArray.getJSONObject(i).getString(
								"id"));
					}
					if (jsonArray.getJSONObject(i).getString("category") != null) {
						place.setCategory(jsonArray.getJSONObject(i).getString(
								"category"));
					}

					if (jsonArray.getJSONObject(i).opt("location") != null) {
						if (jsonArray.getJSONObject(i)
								.getJSONObject("location").opt("city") != null) {
							place.setCity(jsonArray.getJSONObject(i)
									.getJSONObject("location")
									.getString("city"));
						}
						if (jsonArray.getJSONObject(i)
								.getJSONObject("location").opt("country") != null) {
							place.setCountry(jsonArray.getJSONObject(i)
									.getJSONObject("location")
									.getString("country"));
						}
						if (jsonArray.getJSONObject(i)
								.getJSONObject("location")
								.optDouble("latitude") != 0) {
							place.setLatitude(jsonArray.getJSONObject(i)
									.getJSONObject("location")
									.getDouble("latitude"));
						}
						if (jsonArray.getJSONObject(i)
								.getJSONObject("location")
								.optDouble("longitude") != 0) {
							place.setLongitude(jsonArray.getJSONObject(i)
									.getJSONObject("location")
									.getDouble("longitude"));
						}
					}

					listPlace.add(place);
				}
			} else {
				listPlace = null;
			}
		} catch (JSONException e) {
			// Toast.makeText(mContext, mContext.getString(R.string.ERR0007),
			// Toast.LENGTH_LONG).show();
		}
		return listPlace;
	}

}
